package com.zero.insp.services;

import com.github.pagehelper.PageHelper;
import com.zero.insp.common.PPageInfo;
import com.zero.insp.dao.TCompanyExhDao;
import com.zero.insp.dao.TCompanyExhExpandDao;
import com.zero.insp.domain.TCompanyExh;
import com.zero.insp.domain.TCompanyExhExample;
import com.zero.insp.views.CompanyAnalView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author : pengjie
 * @PackageName : com.zero.insp.services
 * @Description : TODO
 * @email : 627799251@qq.com
 * @Date : 2019/3/26 15:14
 */
@Service
public class InspService {

    @Autowired
    private TCompanyExhDao companyExhDao;
    @Autowired
    private TCompanyExhExpandDao companyExhExpandDao;

    public PPageInfo<TCompanyExh> query(TCompanyExhExample example, int page, int size){

        PageHelper.startPage(page,size);

        List<TCompanyExh> cc = companyExhDao.selectByExample(example);

        PPageInfo<TCompanyExh> result = new PPageInfo<>(cc);

        return result;
    }


    public int add(TCompanyExh record){
        return companyExhDao.insertSelective(record);
    }

    public int delete(long insp_id){
        return companyExhDao.deleteByPrimaryKey(insp_id);
    }
    public int edit(TCompanyExh companyExh, long insp_id){
        companyExh.setId(insp_id);
        return companyExhDao.updateByPrimaryKeySelective(companyExh);
    }
    public int edit(TCompanyExh companyExh, String company_name){
        TCompanyExhExample exhExample = new TCompanyExhExample();
        exhExample.createCriteria().andCompanyNameEqualTo(company_name);

        return companyExhDao.updateByExampleSelective(companyExh, exhExample);
    }
    public TCompanyExh findByKey(long insp_id){

        return companyExhDao.selectByPrimaryKey(insp_id);
    }
    public TCompanyExh findByCompanyName(String company_name, String period_num){
        TCompanyExhExample example = new TCompanyExhExample();
        example.createCriteria().andCompanyNameEqualTo(company_name).andPeriodNumEqualTo(period_num);

        List<TCompanyExh> cc = companyExhDao.selectByExample(example);

        if (CollectionUtils.isEmpty(cc)){
            return null;
        }

        return cc.get(0);
    }

    public List<CompanyAnalView> companyAnal(String period_num){
        return companyExhExpandDao.selectAnal(period_num);
    }
}
